package com.yiren.dbaa.module.practice_set.line.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.AddLineContract;
import com.yiren.dbaa.module.practice_set.line.presenter.AddLinePresenter;
import com.yiren.dbaa.module.practice_set.line.view.adapter.ShareLineAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/15 17:27
 * @description: 新增线路页面
 */
public class AddLineActivity extends BaseActivity implements AddLineContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_line_name)
    EditText etLineName;
    @BindView(R.id.et_line_creator)
    EditText etLineCreator;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.lv_share_line)
    ListView lvShareLine;

    private AddLineContract.Presenter addLinePresenter;
    private List<LineInfoEntity> lineListSer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_line);
        ButterKnife.bind(this);
        tvTitle.setText("新增线路");
        Intent intent = getIntent();
        lineListSer = (List<LineInfoEntity>) intent.getSerializableExtra("lineListSer");
        init();
    }

    private void init() {
        addLinePresenter = new AddLinePresenter(this);
        addLinePresenter.loadShareLine(this,"","");
    }

    @OnClick({R.id.iv_back, R.id.tv_select, R.id.ll_collect_line})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_select:
                String lineName = etLineName.getText().toString();
                String userName = etLineCreator.getText().toString();
                addLinePresenter.loadShareLine(this,lineName,userName);
                break;
            case R.id.ll_collect_line:
                Intent intent = new Intent(this, LineEditActivity.class);
                intent.putExtra("pageType", 1);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showShareLine(List<LineInfoEntity> lineList) {
        if (lineList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            tvTips.setText("当前查询条件，线路共享列表为空！");
            lvShareLine.setVisibility(View.GONE);
        } else {
            llNoData.setVisibility(View.GONE);
            lvShareLine.setVisibility(View.VISIBLE);
            ShareLineAdapter shareLineAdapter = new ShareLineAdapter(this, lineList);
            lvShareLine.setAdapter(shareLineAdapter);
            shareLineAdapter.setOnPreviewClickListener(position -> {
                Intent intent = new Intent(this, LineEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("pageType", 3);
                bundle.putSerializable("line", lineList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            });
            shareLineAdapter.setOnDownloadClickListener(position -> {
                addLinePresenter.downLoadLine(lineList.get(position),lineListSer, new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        lineList.get(position).setDownLoad(true);
                        shareLineAdapter.notifyDataSetChanged();
                    }
                });
            });
        }
    }

}